package com.yinhai.activity;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

import com.androidquery.AQuery;
import com.lz.util.Utils;

/**
 * IsDXZDHFActivity 短信自动回复
 * 
 * @author lz
 */
public class IsDXZDHFActivity extends Activity {
	private AQuery aq = null;
	private IsDXZDHFActivity thisRef;
	private AlertDialog popDialog = null;
	String phone = "";
	String groupid = "";
	
	private EditText showDate = null;  
    private EditText showTime = null; 
    private EditText showDate1 = null;  
    private EditText showTime1 = null;
    
    private static final int SHOW_DATAPICK = 0;  
    private static final int SHOW_DATAPICK1 = 6;
    private static final int DATE_DIALOG_ID = 1;
    private static final int DATE_DIALOG_ID1 = 4;
    private static final int SHOW_TIMEPICK = 2;  
    private static final int SHOW_TIMEPICK1 = 7;
    private static final int TIME_DIALOG_ID = 3; 
    private static final int TIME_DIALOG_ID1 = 5; 
    
	private int mYear;    
    private int mMonth;  
    private int mDay;   
    private int mHour;  
    private int mMinute;  

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dxzdhf);
		thisRef = this;
		aq = new AQuery(this);

		CheckBox cbAutoLogin = (CheckBox) findViewById(R.id.cbAutoLogin);
		cbAutoLogin.setChecked(Utils.getAutoIsZDHF(thisRef));
		cbAutoLogin.setOnCheckedChangeListener(cbAutoLoginCheckedChanged);

		aq.id(R.id.layAutoLogin).clicked(layAutoLoginClick);
		
		initializeViews();  
         
        final Calendar c = Calendar.getInstance();  
        mYear = c.get(Calendar.YEAR);    
        mMonth = c.get(Calendar.MONTH);    
        mDay = c.get(Calendar.DAY_OF_MONTH);  
          
        mHour = c.get(Calendar.HOUR_OF_DAY);  
        mMinute = c.get(Calendar.MINUTE);  
	          
	       // setDateTime();   
	        //setTimeOfDay();  

        aq.id(R.id.sms_send).clicked(OnBaoCun);
	}
	

	OnClickListener layAutoLoginClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			aq.id(R.id.cbAutoLogin).click();
		}
	};
	/**
	 * 改变登录设置,保存到配置文件
	 * */
	OnCheckedChangeListener cbAutoLoginCheckedChanged = new OnCheckedChangeListener() {
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			Utils.SaveAutoIsZDHF(thisRef, isChecked);
			if(isChecked){
				Utils.startMsgService(thisRef);
				Utils.showToast(thisRef, "自动回复已开启");
			}else{
				Utils.stopMsgService(thisRef);
				Utils.showToast(thisRef, "自动回复已关闭");
			}
		}
	};
	
	 /** 
     * 初始化控件和UI视图 
     */  
    private void initializeViews(){  
        showDate = (EditText) findViewById(R.id.showdate);    
        showTime = (EditText)findViewById(R.id.showtime);
        showDate1 = (EditText) findViewById(R.id.showdate1);    
        showTime1 = (EditText)findViewById(R.id.showtime1);  
       try {
    	   long t  = Utils.getKSDATE(thisRef);
    	   Calendar c= Calendar.getInstance();
    	   if(t>0){
    	   	   c.setTimeInMillis(t);
    	       String na=c.get(Calendar.YEAR)+"-"+(c.get(Calendar.MONTH) + 1 <10 ? "0"+(c.get(Calendar.MONTH) + 1):c.get(Calendar.MONTH) + 1)+"-"+c.get(Calendar.DAY_OF_MONTH);
               showDate.setText(na);
               showTime.setText(c.get(Calendar.HOUR_OF_DAY)+":"+(c.get(Calendar.MINUTE) < 10 ? "0"+c.get(Calendar.MINUTE):c.get(Calendar.MINUTE) )); 
    	   }
	   	   
          
    	   long t1  = Utils.getJSDATE(thisRef);
           if(t1>0){
        	   c.setTimeInMillis(t1);
       	       String na1=c.get(Calendar.YEAR)+"-"+(c.get(Calendar.MONTH) + 1 <10 ? "0"+(c.get(Calendar.MONTH) + 1):c.get(Calendar.MONTH) + 1)+"-"+c.get(Calendar.DAY_OF_MONTH);
               showDate1.setText(na1);
               showTime1.setText(c.get(Calendar.HOUR_OF_DAY)+":"+(c.get(Calendar.MINUTE) < 10 ? "0"+c.get(Calendar.MINUTE):c.get(Calendar.MINUTE) ));
           }
   	       
		} catch (Exception e) {
			e.printStackTrace();
		}
        
        aq.id(R.id.edtContent).text(Utils.getDXContent(thisRef));
        
        showDate.setOnClickListener(new View.OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
               Message msg = new Message();   
              // if (pickDate.equals((Button) v)) {    
                  msg.what = IsDXZDHFActivity.SHOW_DATAPICK;    
             //  }    
               thisRef.dateandtimeHandler.sendMessage(msg);   
            }  
        }); 
        showDate1.setOnClickListener(new View.OnClickListener() {  
            
            @Override  
            public void onClick(View v) {  
               Message msg = new Message();   
              // if (pickDate.equals((Button) v)) {    
                  msg.what = IsDXZDHFActivity.SHOW_DATAPICK1;    
             //  }    
               thisRef.dateandtimeHandler.sendMessage(msg);   
            }  
        });
          
        showTime.setOnClickListener(new View.OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
               Message msg = new Message();   
             //  if (pickTime.equals((Button) v)) {    
                  msg.what = IsDXZDHFActivity.SHOW_TIMEPICK;    
              // }    
               thisRef.dateandtimeHandler.sendMessage(msg);   
            }  
        }); 
        showTime1.setOnClickListener(new View.OnClickListener() {  
            
            @Override  
            public void onClick(View v) {  
               Message msg = new Message();   
             //  if (pickTime.equals((Button) v)) {    
                  msg.what = IsDXZDHFActivity.SHOW_TIMEPICK1;    
              // }    
               thisRef.dateandtimeHandler.sendMessage(msg);   
            }  
        });
    }
	
    
    /** 
     * 设置日期 
     */  
    private void setDateTime(){  
       final Calendar c = Calendar.getInstance();    
         
       mYear = c.get(Calendar.YEAR);    
       mMonth = c.get(Calendar.MONTH);    
       mDay = c.get(Calendar.DAY_OF_MONTH);   
    
       updateDateDisplay();   
    }  
      
    /** 
     * 更新日期显示 
     */  
    private void updateDateDisplay(){  
       showDate.setText(new StringBuilder().append(mYear).append("-")  
               .append((mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)).append("-")  
               .append((mDay < 10) ? "0" + mDay : mDay));   
    }  
      
    
    /** 
     * 更新日期显示 
     */  
    private void updateDateDisplay1(){  
       showDate1.setText(new StringBuilder().append(mYear).append("-")  
               .append((mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)).append("-")  
               .append((mDay < 10) ? "0" + mDay : mDay));   
    }
    
    
    /**  
     * 日期控件的事件  
     */    
    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {    
    
       public void onDateSet(DatePicker view, int year, int monthOfYear,    
              int dayOfMonth) {    
           mYear = year;    
           mMonth = monthOfYear;    
           mDay = dayOfMonth;    
  
           updateDateDisplay();  
       }    
    };   
    
    /**  
     * 日期控件的事件  
     */    
    private DatePickerDialog.OnDateSetListener mDateSetListener1 = new DatePickerDialog.OnDateSetListener() {    
    
       public void onDateSet(DatePicker view, int year, int monthOfYear,    
              int dayOfMonth) {    
           mYear = year;    
           mMonth = monthOfYear;    
           mDay = dayOfMonth;    
  
           updateDateDisplay1();  
       }    
    };
      
    /** 
     * 设置时间 
     */  
    private void setTimeOfDay(){  
       final Calendar c = Calendar.getInstance();   
       mHour = c.get(Calendar.HOUR_OF_DAY);  
       mMinute = c.get(Calendar.MINUTE);  
       updateTimeDisplay();  
    }  
      
    /** 
     * 更新时间显示 
     */  
    private void updateTimeDisplay(){  
       showTime.setText(new StringBuilder().append(mHour).append(":")  
               .append((mMinute < 10) ? "0" + mMinute : mMinute));   
    }  
      
    
    /** 
     * 更新时间显示 
     */  
    private void updateTimeDisplay1(){  
       showTime1.setText(new StringBuilder().append(mHour).append(":")  
               .append((mMinute < 10) ? "0" + mMinute : mMinute));   
    }
    
    /** 
     * 时间控件事件 
     */  
    private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {  
          
        @Override  
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {  
            mHour = hourOfDay;  
            mMinute = minute;  
              
            updateTimeDisplay();  
        }  
    };  
    
    /** 
     * 时间控件事件 
     */  
    private TimePickerDialog.OnTimeSetListener mTimeSetListener1 = new TimePickerDialog.OnTimeSetListener() {  
          
        @Override  
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {  
            mHour = hourOfDay;  
            mMinute = minute;  
              
            updateTimeDisplay1();  
        }  
    };
      
    @Override    
    protected Dialog onCreateDialog(int id) {    
       switch (id) {    
       case DATE_DIALOG_ID:    
           return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,    
                  mDay);  
       case TIME_DIALOG_ID:  
           return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, true);
       case DATE_DIALOG_ID1:    
           return new DatePickerDialog(this, mDateSetListener1, mYear, mMonth,    
                  mDay);  
       case TIME_DIALOG_ID1:  
           return new TimePickerDialog(this, mTimeSetListener1, mHour, mMinute, true);
       }  
             
       return null;    
    }    
    
    @Override    
    protected void onPrepareDialog(int id, Dialog dialog) {    
       switch (id) {    
       case DATE_DIALOG_ID:    
           ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);    
           break;  
       case TIME_DIALOG_ID:  
           ((TimePickerDialog) dialog).updateTime(mHour, mMinute);  
           break;  
       case DATE_DIALOG_ID1:    
           ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);    
           break;  
       case TIME_DIALOG_ID1:  
           ((TimePickerDialog) dialog).updateTime(mHour, mMinute);  
           break;  
       }  
    }    
    
    /**  
     * 处理日期和时间控件的Handler  
     */    
    Handler dateandtimeHandler = new Handler() {  
    
       @SuppressWarnings("deprecation")
	@Override    
       public void handleMessage(Message msg) {    
           switch (msg.what) {    
           case IsDXZDHFActivity.SHOW_DATAPICK:    
               showDialog(DATE_DIALOG_ID);    
               break;   
           case IsDXZDHFActivity.SHOW_TIMEPICK:  
               showDialog(TIME_DIALOG_ID);  
               break; 
           case IsDXZDHFActivity.SHOW_DATAPICK1:    
               showDialog(DATE_DIALOG_ID1);    
               break;   
           case IsDXZDHFActivity.SHOW_TIMEPICK1:  
               showDialog(TIME_DIALOG_ID1);  
               break;  
           }    
       }    
    
    };   

	
	/**
	 * 
	 * 保存
	 */
	
    OnClickListener OnBaoCun = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			try {

				String  edtContent = aq.id(R.id.edtContent).getText().toString();
				if(edtContent.equals("")){
					Utils.showToast(thisRef, "不能为空！");
					return;
				}else{
					Utils.SaveDXContent(thisRef, edtContent);
				}
				
				String showDateK = showDate.getText().toString();
				String showDateK1 = showDate1.getText().toString();
				String showTimeK = showTime.getText().toString();
				String showTimeK1 = showTime1.getText().toString();
				long t = 0;
				long t1 = 0;
				if(showDateK.equals("") || showTimeK.equals("")){
					Utils.showToast(thisRef, "开始日期或时间不能为空！");
					return;
				}else{
				    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
				    t= sdf.parse(showDateK+" "+showTimeK).getTime();
				    Utils.SaveKSDATE(thisRef, t);
				    
				}
				 
				if(showDateK1.equals("") || showTimeK1.equals("") ){
					Utils.showToast(thisRef, "结束日期或时间不能为空！");
					return;
				}else{
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
					t1 = sdf.parse(showDateK1+" "+showTimeK1).getTime();
				    Utils.SaveJSDATE(thisRef, t1);
				}
				
				if( t1 - t >0){
				    Utils.showToast(thisRef, "保存成功！");
				}else{
				    Utils.showToast(thisRef, "结束日期不能小于开始日期！");
                    return;
				}
				 
				
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};
	
	
	
	

	/**
	 * 返回
	 * 
	 * @param view
	 */
	public void doBack(View view) {
		onBackPressed();
	}

}
